package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /**
     * 按菜单名模糊查询所有菜单(包括子菜单)
     * @param queryPageBean
     * @return
     */
    @RequestMapping(value = "/findByCondition",method = RequestMethod.POST)
    public Result findByCondition(@RequestBody QueryPageBean queryPageBean){
        try {
            List<Menu> menuList = menuService.findByCondition(queryPageBean.getQueryString());
            return new Result(true, MessageConstant.GET_MENU_LIST_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_LIST_SUCCESS);
        }
    }

    /**
     * 查询所有父菜单
     * @return
     */
    @RequestMapping(value = "/findParentMenuList",method = RequestMethod.GET)
    public Result findParentMenuList(){
        try {
            List<Menu> menuList = menuService.findParentMenuList();
            return new Result(true, MessageConstant.GET_MENU_LIST_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_LIST_FAIL);
        }
    }

    /**
     * 查询所有菜单，以树形二级结构返回
     * @return
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        try {
            List<Menu> menuList = menuService.findAll();
            return new Result(true, MessageConstant.GET_MENU_LIST_SUCCESS,menuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MENU_LIST_FAIL);
        }
    }


    /**
     * 添加菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody Menu menu){
        try {
            menuService.add(menu);
            return new Result(true,MessageConstant.ADD_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_MENU_FAIL);
        }
    }

    /**
     * 根据ID查询菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(Integer id){
        try {
            Menu menu = menuService.findById(id);
            return new Result(true,MessageConstant.GET_MENU_LIST_SUCCESS,menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_LIST_FAIL);
        }
    }

    /**
     * 编辑菜单
     * @param menu
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Result edit(@RequestBody Menu menu){
        try {
            menuService.edit(menu);
            return new Result(true,MessageConstant.EDIT_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_MENU_FAIL);
        }
    }

    /**
     * 根据ID删除菜单
     * @param id
     * @return
     */
    @RequestMapping(value = "/deleteById",method = RequestMethod.GET)
    public Result deleteById(Integer id){
        try {
            menuService.deleteById(id);
            return new Result(true,MessageConstant.DELETE_MENU_SUCCESS);
        }catch (RuntimeException e){
            e.printStackTrace();
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_MENU_FAIL);
        }
    }

    @RequestMapping(value = "/validateName",method = RequestMethod.POST)
    public Result validateName(@RequestBody Map map){
        try {
            List<Menu> menuList = menuService.validateName(map.get("menuName").toString());
            if (menuList != null && menuList.size() > 0){
                return new Result(true,MessageConstant.MENU_IS_EXIST);
            }else {
                return new Result(false,MessageConstant.GET_MENU_LIST_FAIL);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_LIST_FAIL);
        }
    }
}
